package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.DriverMapper;
import com.jiyun.pojo.DcDriver;
import com.jiyun.service.DriverService;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, DcDriver> implements DriverService {
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public Integer findCarId(Integer id) {
        return driverMapper.findCarId(id);
    }

    @Override
    public DcDriver findDriverByTel(String tel) {
        return driverMapper.findDriverByTel(tel);
    }

    @Override
    public R tixin(String value1, Integer id) {
        DcDriver driver = driverMapper.selectById(id);
        System.out.println("driver ====== " + driver);
        if (driver == null) {
            // 处理 driver 为 null 的情况，例如返回错误信息
            return R.error();
        }

        // 将 value1 转换为 BigDecimal
        BigDecimal inputMoney = new BigDecimal(value1);
        System.out.println("inputMoney =========== " + inputMoney);

        // 获取数据库中的 Summoney
        BigDecimal summoney = driver.getTotalAssets();
        System.out.println("summoney =========== " + summoney);

        // 检查 summoney 是否为 null
        if (summoney == null) {
            // 处理 summoney 为 null 的情况，例如返回错误信息或设置默认值
            return R.error();
            // 或者设置默认值
            // summoney = BigDecimal.ZERO;
        }

        // 将数据库的 Summoney 减去 inputMoney 提现金额
        BigDecimal newSummoney = summoney.subtract(inputMoney);
        System.out.println("newSummoney =========== " + newSummoney);

        driver.setTotalAssets(newSummoney);

        driverMapper.updateById(driver);
        return R.ok();
    }

    @Override
    public DcDriver sjById(Integer id) {
        return driverMapper.selectById(id);
    }

    @Override
    public void updateStatus(Integer id) {
        driverMapper.updateStatus(id);
    }

    @Override
    public void updateStatus2(Integer id) {
        driverMapper.updateStatus2(id);
    }

    @Override
    public void cancelOrderCount(Integer id) {
        driverMapper.cancelOrderCount(id);
    }

    @Override
    public Integer findCount(Integer id) {
        return driverMapper.findCount(id);
    }

    @Override
    public void updateDriverMoney(Integer id) {
        driverMapper.updateDriverMoney(id);
    }

    @Override
    public void saveDriverMaoney(Integer id, BigDecimal totalAssets) {
        driverMapper.saveDriverMaoney(id, totalAssets);
    }
}
